﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace vITs.Site.Models.DataModels
{
    public static class VitsExtensionMethods
    {
        public static AssignmentStatus GetStatus(this Assignment assignment)
        {
            AssignmentStatus status;
            if (!Enum.TryParse<AssignmentStatus>(assignment.Status.ToString(), out status))
            {
                return AssignmentStatus.AssignmentDraft;
            }

            return status;
        }

        public static string GetFullName(this User user)
        {
            return String.Format("{0} {1}", user.GivenName, user.Surname);
        }

        public static Double GetTransportationCost(this Trip trip)
        {
            return (trip.GetTransportationMode() == TransportationMode.Car ? trip.TransportationCost * 18.5 : trip.TransportationCost);
        }

        public static Double GetTotalCost(this IEnumerable<PerDiem> perDiems)
        {
            var pds = new VitsDB().PerDiemSpecifications;

            Double retval = 0d;

            foreach (PerDiem pd in perDiems)
            {
                PerDiemSpecification spec = pds.SingleOrDefault(p => p.Year == pd.Date.Year && p.Country == pd.Country);
                if (spec != null)
                {
                    var sum = spec.Sum;

                    if (pd.Breakfast) sum -= 44;
                    if (pd.Lunch) sum -= 77;
                    if (pd.Dinner) sum -= 77;

                    retval += sum;
                }
            }

            return retval;
        }

        public static Double GetTotalCost(this IEnumerable<Trip> trips)
        {
            Double retval = 0d;

            foreach (Trip trip in trips)
            {
                retval += trip.GetTransportationCost();
            }

            return retval;
        }

        public static Double GetTotalCost(this IEnumerable<Expense> expenses)
        {
            Double retval = 0d;

            foreach (Expense exp in expenses)
            {
                retval += exp.ExchangeRate * exp.Sum;
            }

            return retval;
        }

        public static Double GetTotalCost(this Assignment assignment)
        {
            Double retval = -assignment.Advance;

            retval += assignment.Trips.GetTotalCost();
            retval += assignment.PerDiems.GetTotalCost();
            retval += assignment.Expenses.GetTotalCost();

            return retval;
        }

        public static void SetStatus(this Assignment assignment, AssignmentStatus mode)
        {
            int i = (int)mode;
            if (i > 0)
            {
                byte b;
                if (byte.TryParse(i.ToString(), out b))
                {
                    assignment.Status = b;
                }
            }
        }

        public static Boolean IsEditable(this Assignment assignment)
        {
            Boolean retval = true;
            AssignmentStatus status = assignment.GetStatus();

            if (status == AssignmentStatus.AssignmentDraft ||
                status == AssignmentStatus.AssignmentRejected ||
                status == AssignmentStatus.AdvanceRequested ||
                status == AssignmentStatus.AdvanceRejected ||
                status == AssignmentStatus.Submitted ||
                status == AssignmentStatus.Rejected ||
                status == AssignmentStatus.Finalized)
            {
                retval = false;
            }


            return retval;
        }

        public static TransportationMode GetTransportationMode(this Trip trip)
        {
            TransportationMode mode;
            if (!Enum.TryParse<TransportationMode>(trip.TransportationMode.ToString(), out mode))
            {
                return TransportationMode.Car;
            }

            return mode;
        }

        public static void SetTransportationMode(this Trip trip, TransportationMode mode)
        {
            int i = (int)mode;
            if (i > 0)
            {
                byte b;
                if (byte.TryParse(i.ToString(), out b))
                {
                    trip.TransportationMode = b;
                }
            }
        }
    }
}